package org.kele.sc2020.proivder.controller;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.kele.sc2020.common.dto.Dog;
import org.kele.sc2020.common.dto.KeleResult;
import org.kele.sc2020.proivder.exception.BizRuntimeException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
@RestController
public class ProviderController {

    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger(0);

    @GetMapping("/inner/get-info")
    public String getInfoInner() throws Exception {
        log.info("becall:{}", ATOMIC_INTEGER.incrementAndGet());

        try {
            TimeUnit.SECONDS.sleep(0L);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return "ok kele";
//       throw new BizRuntimeException("fuck up");
    }

    @GetMapping("/inner/test-retry")
    public KeleResult<Object> testRetry() {
        ATOMIC_INTEGER.incrementAndGet();
        if (ATOMIC_INTEGER.get() % 2 == 0) {
            System.out.println("模拟调用成功...");
            return KeleResult.success("ok la");
        } else{
            System.out.println("模拟调用失败");
            return KeleResult.rpcFail();
        }

    }

//    @Resource
//    RabbitTemplate rabbitTemplate;


//    @GetMapping("/test/msq")
//    public void sendtomq() throws IOException {
//        rabbitTemplate.convertAndSend(CONFIRM_EXCHANGE, ROUTING_KEY, "hello  i am xukele");
//    }

//    @GetMapping("/test/msq2")
//    public void sendtomq2() throws IOException {
//        CorrelationData correlationData = new CorrelationData(IdUtil.fastSimpleUUID());
//        String messagestr = "hello  i am xukele22";
//        // 发送前加入消息表, 如果 confirm失败 或return 失败,则 修改标记为 等待重试;
//
//        rabbitTemplate.convertAndSend(CONFIRM_EXCHANGE, ROUTING_KEY, messagestr, message -> {
//            message.getMessageProperties().setExpiration("10000");// 设置消息十秒后过期
//            message.getMessageProperties().setMessageId(correlationData.getId()); // 设置messageId  和correlationData的id保持一致;
//            return message;
//        }, correlationData);
//    }


//    @GetMapping("/test/msq3")
//    public void sendtomq3() throws IOException {
//        rabbitTemplate.convertAndSend(CONFIRM_EXCHANGE, "dtokey", new Dog().setColor("黄色").setName("大黄"));
//    }
}
